﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RM.WinClient.RMServices;
///using RM.WinClient.RMQuestionService;
using System.Timers;
using System.IO;
using System.Diagnostics;
using Microsoft.Windows.Controls;
using System.Windows.Threading;
using RM.WinClient.Utilities;

namespace RM.WinClient
{
    public partial class CandidateList : Page
    {
        #region Declaration
        Timer ticks = new Timer(1000);
        #endregion

        #region Constructor
        public CandidateList()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CandidateList_Loaded);
        }
        #endregion

        #region Private Methods

        private void SetGridColumnsWidth()
        {
            double actualWidth = this.dataGrid.ActualWidth;
            double columnsWidth = actualWidth;
            this.dataGrid.Columns[0].Width = new DataGridLength((columnsWidth * 25) / 100);
            this.dataGrid.Columns[1].Width = new DataGridLength((columnsWidth * 30) / 100);
            this.dataGrid.Columns[2].Width = new DataGridLength((columnsWidth * 30) / 100);
            //this.dataGrid.Columns[3].Width = new DataGridLength((columnsWidth * 10) / 100);
        }

        #endregion

        #region Loaded

        void CandidateList_Loaded(object sender, RoutedEventArgs e)
        {
            CandidateServiceClient candidateServices = new CandidateServiceClient();
            this.dataGrid.ItemsSource = candidateServices.Candidate_GetAll();
            progressBar.Visibility = Visibility.Collapsed;

            QuestionServiceClient objQuestionServiceClient = new QuestionServiceClient();
            cmbQuestionSets.ItemsSource = objQuestionServiceClient.Question_GetAll();

        }

        #endregion

        #region Search
        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            CandidateServiceClient candidateServices = new CandidateServiceClient();
            this.dataGrid.ItemsSource = candidateServices.Candidate_GetAllBySearch(txtSearch.Text.Trim());
        }
        #endregion

        #region Show Resume
        private void OnHyperlinkClick(object sender, RoutedEventArgs e)
        {
            TextBlock text = sender as TextBlock;
            CandidateServiceClient candidateServices = new CandidateServiceClient();
            Candidate candidate = candidateServices.Candidate_GetById(int.Parse(text.Tag.ToString()));
            if (candidate != null && candidate.Resume != null)
            {
                long length = candidate.Resume.Length;
                byte[] data = candidate.Resume;
                FileStream fs = new FileStream("TempDoc.doc", FileMode.OpenOrCreate);
                fs.Write(data, 0, (int)length);
                fs.Flush();
                fs.Close();
                Process.Start("winword.exe", "TempDoc.doc");
            }
        }
        #endregion

        #region Grid Loaded

        private void dataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            SetGridColumnsWidth();
        }

        #endregion

        #region Start button Click

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {

            Candidate objCandidate = dataGrid.SelectedItem as Candidate;
            if (objCandidate == null)
            {
                //Show Error Message
                ErrorDialog.Show(Constants.ErrCandidate);
                return;
            }

            Question objQuestion = cmbQuestionSets.SelectedItem as Question;
            if (objQuestion == null)
            //Show Error messge
            {
                ErrorDialog.Show(Constants.ErrQuestion);
                return;
            }



            App.Current.Resources.Add(Constants.SelectedCandidate, objCandidate);
            NavigationService.Navigate(new QuestionDetails(objQuestion));

            TestTimer.Start();
            Util.LogMessage(new OperationLog() { Entity = "Test", Data = objCandidate.Id.ToString(), Message = "Test Started for candidate ID" + objCandidate.Id + ";" });
        }

        #endregion

        #region View Resume

        private void ViewResume_Click(object sender, RoutedEventArgs e)
        {
            Button btnResume = sender as Button;
            CandidateServiceClient candidateServices = new CandidateServiceClient();
            Candidate candidate = candidateServices.Candidate_GetById(int.Parse(btnResume.Tag.ToString()));
            if (candidate != null && candidate.Resume != null)
            {
                long length = candidate.Resume.Length;
                byte[] data = candidate.Resume;
                FileStream fs = new FileStream("TempDoc.doc", FileMode.OpenOrCreate);
                fs.Write(data, 0, (int)length);
                fs.Flush();
                fs.Close();
                Process.Start("winword.exe", "TempDoc.doc");
            }

        }

        #endregion

    }
}
